﻿$(function () {
    new OptBase();
});

//全局参数 
var Parameters = {
    func: undefined,
    exitLogin: "/Admin/Home/ExitLogin",
    login: "/Admin/Login/Index",
    loadMenus: "/Admin/Home/LoadMenus",
};

var OptBase = function () {
    //可以把相同的大类操作逻辑写到一个里面去
    this.initPage();
    this.bindEvent();
};

//事件调用
OptBase.prototype = {
    //初始化事件页面
    initPage: function () {

        Parameters.func = new HomeFunc();
        //界面操作工具类
        Parameters.func.pageTools = new PageTools();

        Parameters.func.loadFun();

    },
    //页面事件
    bindEvent: function () {

        //修改密码
        $("#updatePwd").click(function () {
            // addTab("修改密码", "/PayPlatform/admin/updatePwd", "icon");
        });

        //保存密码
        $("#btnSave").click(function () {
            Parameters.func.savePassword();
        });

        //退出系统
        $("#exit").click(function () {
            $.messager.confirm('系统提示', '您确定要退出系统吗?', function (r) {
                if (r) {
                    fs.AjaxPost({
                        url: Parameters.exitLogin,
                        data: "",
                        success: function (result) {
                            if (result.Success) {
                                location.href = Parameters.login;
                            }
                        }
                    });
                }
            });
        });
    }
};

///逻辑模块
function HomeFunc() {
    /// <summary>
    /// 初始化左边的菜单
    /// </summary>
    HomeFunc.prototype.loadFun = function () {
        fs.AjaxPost({
            url: Parameters.loadMenus,
            data: "",
            success: function (result) {
                if (result.Success) {
                    var data = result.Data;
                    Parameters.func.initMenu(data);
                }
            }
        });
    };

    ///初始化左侧
    HomeFunc.prototype.initMenu = function (data) {
        var accordion = ".easyui-accordion1";
        $(".easyui-accordion").empty();
        var menulist = "";
        $.each(data, function (i, n) {
            if (n._parentId == "") {
                menulist += '<div title="' + n.Name + '" icon="' + n.Icon + '" style="overflow:auto;">';
                menulist += '<ul>';
                $.each(data, function (j, o) {
                    if (o._parentId == n.Code) {
                        menulist += '<li><div><a ref="' + o.Code + '"  href="#"   rel="' + o.LinkRote + '"><span class="icon ' + o.Icon + '" >&nbsp;</span><span class="nav">' + o.Name + '</span></a></div></li>';
                    }
                });
                menulist += '</ul></div>';
            }
        });
        $(accordion).append(menulist);
        $(accordion + ' li a').click(function () {
            var tabTitle = $(this).children('.nav').text();
            var url = $(this).attr("rel");
            var Id = $(this).attr("ref");
            var icon = $(this).find("span").eq(0).attr("class");
            //var icon = getIcon(Id, data);
            addTab(tabTitle, url, icon);
            $(accordion + ' li div').removeClass("selected");
            $(this).parent().addClass("selected");
        }).hover(function () {
            $(this).parent().addClass("hover");
        }, function () {
            $(this).parent().removeClass("hover");
        });
        //导航菜单绑定初始化
        $(accordion).accordion();
    };

    /**
	 * 保存用户密码信息
	 */
    HomeFunc.prototype.savePassword = function () {

        var sPassWord = $("#txtPassword").val();
        var srPassWord = $("#txtrPassword").val();
        var oldPassword = $("#txtOldPassword").val();

        if (!Parameters.func.pageTools.judge.isNotEmpty(oldPassword)) {
            fs.PopUpWindow.alerts("旧密码不能为空!", "warning");
            return;
        }
        if (sPassWord != srPassWord) {
            fs.PopUpWindow.alerts("前后两次密码输入不正确!", "warning");
            return;
        }
        if (!Parameters.func.pageTools.judge.isPassWord(sPassWord)) {
            fs.PopUpWindow.alerts("密码为6到18位的数字或字母!", "warning");
            return;
        }
        var json = {
            sOldPsw: oldPassword,
            sNewPsw: sPassWord
        };
        fs.AjaxPost({
            url: "/PayPlatform/admin/savePassword.do",
            data: json,
            success: function (result) {
                if (result.succ) {
                    fs.PopUpWindow.alerts(result.message, "info");
                }
            }
        });
    };
};
